//
//  SearchRankController.m
//  Cabinet
//
//  Created by Florent Montsegur on 23/08/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SearchRankController.h"

@implementation SearchRankController

@synthesize mRanksView;
@synthesize mManagedObjectContext;

- (id)initWithWindow:(NSWindow *)window
{
    self = [super initWithWindow:window];
    if (self) {
        // Initialization code here.
        mRanks = [[NSMutableArray alloc] init];
    }
    
    return self;
}

- (void)windowDidLoad
{
    [super windowDidLoad];
    
    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}

- (void)dealloc
{
    if(mRanks != nil)
        [mRanks removeAllObjects];
}

- (NSString*)windowNibName
{
	return @"SearchRank";
}


- (void)withParentWindow:parentWindow
{
    mSelectedIndex = -1;
	NSWindow* window = [self window];
	
	[NSApp beginSheet:window modalForWindow:parentWindow modalDelegate:nil didEndSelector:nil contextInfo:nil];
	[NSApp runModalForWindow:window];
	// sheet is up now...
	
	[NSApp endSheet:window];
	[window orderOut:self];
}


- (IBAction)okAction:sender {
    
    NSLog(@"On OK");
    
    mSelectedIndex = [mRanksView selectedRow];
    
    mResult = NSOKButton;
    [self closeWindow];
}

- (IBAction)annulerAction:sender {
    
    NSLog(@"On Annuler");
    
    mResult = NSCancelButton;
    [self closeWindow];
}

- (void) closeWindow
{
    [[self mRanksView] setDataSource:nil];
    [[self window] orderOut:nil];
    [NSApp stopModal];    
}

- (int) returnCode
{
    return mResult;
}

- (void) setRanks:(NSArray *)aRanksList
{
    [mRanks removeAllObjects];
    
    if(aRanksList == nil)
        return;
    
    [mRanks addObjectsFromArray:aRanksList];    
    [mRanksView reloadData];
}

- (Rang *)selectedRank
{
    return [self getRankAtIndex:mSelectedIndex];
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView{
    return [self count];
}

- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex{
    
    if( rowIndex >= [self count])
        return @"";
    
    Rang *rang = [mRanks objectAtIndex:rowIndex];
    if(rang !=nil)
        return [rang Label];
    
    return @"";
}

- (NSInteger) count{
    return [mRanks count];
}

- (Rang *)getRankAtIndex:(NSInteger)aRowIndex
{
    if(aRowIndex<0) 
        return nil;
    
    if(aRowIndex>=[self count]) 
        return nil;
    
    return [mRanks objectAtIndex:aRowIndex];
}

- (IBAction)supprimerAction:sender
{
    Rang * selectedRang = [self selectedRank];
    if(selectedRang == nil)
        return;
    
    NSAlert * alert = [NSAlert alertWithMessageText:@"Attention"
                                      defaultButton:@"Supprimer"
                                    alternateButton:@"Annuler"
                                        otherButton:nil informativeTextWithFormat:[NSString stringWithFormat:@"%@ sera supprimé définitivement", [selectedRang Label]] ];
    
    // should we try again?
    if ([alert runModal] == NSOKButton)
    {
        [mRanks removeObject:selectedRang];
        [mRanksView reloadData];
        
        [[self mManagedObjectContext] deleteObject:selectedRang];
        
        NSLog(@"Supprimé !!");
    }
}



@end
